﻿//using FeatureManagement;
using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Features;
using Volo.Abp.MultiTenancy;
using Xms.Abp.Application.Services;
using Xms.Abp.Authorization;
using Xms.Abp.Data;
using Xms.Abp.Domain.Repositories;

namespace TenantManagement;

[PermissionsUse(typeof(TenantPermissions))]
public class TenantAppService : XmsCrudAppService<Tenant, TenantResult, Guid, TenantsPagedParameter, TenantCreateParameter, TenantUpdateParameter>, ITenantAppService
{
    protected IDistributedEventBus DistributedEventBus => LazyInject<IDistributedEventBus>();
    //protected ITenantCacheContext TenantCacheContext => LazyInject<ITenantCacheContext>();
    //protected IFeatureManagementStore FeatureStore => LazyInject<IFeatureManagementStore>();
    public TenantAppService(IXmsRepository<Tenant, Guid> repository) : base(repository)
    {
    }

    protected virtual string ManageFeaturesPolicyName => GetPolicy();

    protected override Func<TenantCreateParameter, Tenant> EntityCreator
        => input => new Tenant(GuidGenerator.Create(), input.Name, input.DisplayName);

    protected override async Task<IQueryable<Tenant>> CreateFilteredQueryAsync(TenantsPagedParameter input)
    {
        if (input.Sorting.IsNullOrWhiteSpace())
        {
            input.Sorting = nameof(Tenant.Name);
        }
        return (await base.CreateFilteredQueryAsync(input))
            .WhereIf(!input.Filter.IsNullOrWhiteSpace(), r => r.Name == input.Filter || r.DisplayName.Contains(input.Filter));
    }

    protected override async Task AfterCreateAsync(Tenant entity, TenantCreateParameter input)
    {
        await DistributedEventBus.PublishAsync(new TenantCreatedEto
        {
            Id = entity.Id,
            Name = entity.Name,
            Properties =
            {
                { nameof(input.AdminEmailAddress), input.AdminEmailAddress },
                { nameof(input.AdminPhoneNumber), input.AdminPhoneNumber },
                { nameof(input.AdminPassword), input.AdminPassword },
            }
        });
    }

    //protected override async Task AfterUpdateAsync(Tenant entity, TenantUpdateParameter input)
    //{
    //    await RemoveFromCacheAsync(entity);
    //}

    //protected override async Task AfterToggleEnableAsync(Tenant entity, bool input)
    //{
    //    await RemoveFromCacheAsync(entity);
    //}

    //protected override async Task AfterDeleteAsync(Tenant entity)
    //{
    //    await RemoveFromCacheAsync(entity);
    //}

    //protected virtual async Task RemoveFromCacheAsync(Tenant entity)
    //{
    //    var tenantKey1 = TenantStore.CalculateCacheKey(entity.Id, null);
    //    var tenantKey2 = TenantStore.CalculateCacheKey(null, entity.Name);
    //    await TenantCacheContext.Tenants.RefreshManyAsync(new[] { tenantKey1, tenantKey2 });
    //}

    //public async Task<IEnumerable<NameValue>> GetFeaturesAsync(Guid id)
    //{
    //    var features = await FeatureStore.GetAllAsync(TenantFeatureValueProvider.ProviderName, id.ToString());
    //    return features;
    //}

    //public async Task UpdateFeaturesAsync(Guid id, IEnumerable<NameValue> nameValues)
    //{
    //    await FeatureStore.SetAsync(TenantFeatureValueProvider.ProviderName, id.ToString(), nameValues.Select(r => new FeatureNameValue(r.Name, r.Value)).ToList());
    //}

    //public async Task DeleteAllFeaturesAsync(Guid id)
    //{
    //    await FeatureStore.DeleteAllAsync(TenantFeatureValueProvider.ProviderName, id.ToString());
    //}
}
